<?php
class DictAction extends BaseAction {
	
		public function index() {
				$parentId = intval($_GET['parentId']);
				if(empty($parentId))$parentId=0;
				$listdict = service('Dict')->selectByParentId($parentId);
				$this->assign('listdict', $listdict);
				$this->assign('parentId', $parentId);
				$this->display();
		}
	
	
	
	public function addCategory() {
			$pid = intval($_GET['pid']);
			if(empty($pid))$pid=0;
			$this->assign('pid', $pid);
			$this->display('edit');
	}
	
	public function editCategory() {
			$dict = M('Dict')->where('id='.$_GET['id'])->find();
			$this->assign('pid', $dict['pid']);
			$this->assign('dict', $dict);
			$this->display('edit');
	}
	
	public function doAddCategory() {
		$map['pid'] = intval($_POST['pid']);
		$map['desc'] = $_POST['desc'];
		$map['code'] = $_POST['code'];
		$map['order'] = $_POST['order'];
		if(empty($map['pid']))$map['pid']=0;
		
		$DictService = service('Dict');
		
		//检查用户输入的code是否已经存在
		$tmpDict = $DictService->findByCode($map['code']);
		if (!empty($tmpDict)) {
				exit(json_encode(array('status'=>'-1','title'=>'','msg'=>'信息录入错误，code已存在！'))); 
		}
		
		$result = $DictService->createDict($map);
		if($result){
				exit(json_encode(array('status'=>'1','title'=>'','msg'=>'添加成功！'))); 
		}
		exit(json_encode(array('status'=>'0','title'=>'','msg'=>'添加失败！'))); 
	}
	
	public function doEditCategory() {
		$map['id'] = intval($_POST['id']);
		$map['pid'] = intval($_POST['pid']);
		$map['desc'] = $_POST['desc'];
		$map['code'] = $_POST['code'];
		$map['order'] = $_POST['order'];
		//if(empty($map['pid']))$map['pid']=0;
		
		$DictService = service('Dict');
		
		//检查用户输入的code是否已经存在
		$tmpDict = $DictService->findByCode($map['code']);
		if (!empty($tmpDict)&&$tmpDict['id']!=$map['id']) {
				exit(json_encode(array('status'=>'-1','title'=>'','msg'=>'信息录入错误，code已存在！'))); 
		}
		
		$result = $DictService->updateDict($map);
		if($result){
				exit(json_encode(array('status'=>'1','title'=>'','msg'=>'修改成功！'))); 
		}
		exit(json_encode(array('status'=>'0','title'=>'','msg'=>'修改失败！'))); 
	}
	
	
	public function doDeleteCategory() {
		$ids	= explode(',', t($_POST['ids']));
		if (empty($ids)) {
			echo 0;
			return ;
		}
		$result = service('Dict')->deleteDict($ids); 
		echo $result ? '1' : '0';
	}
	
	public function getXML(){
		$code = $_GET['code'];
		$result = service('Dict')->getAllDictByCode($code);
		$dict = service('Dict')->findByCode($code);
		$i = 0;
		$result1[$i] = $dict;
		foreach($result as $vo){ 
				$i = $i+1;
      	$result1[$i] = $vo;   
		}
		exit(json_encode($result1)); 
	}
	
}
?>